/**
* Copyright 2018 Huawei Technologies Co.,Ltd.
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License.  You may obtain a copy of the
* License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations under the License.
*
* 
* sdk reference linking：https://support.huaweicloud.com/sdkreference-ocr/ocr_04_0016.html
**/

package com.example.demo.huawei.OCRDemo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.huawei.ocr.HWOcrClientAKSK;
import com.example.demo.huawei.ocr.HWOcrClientToken;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;

import java.io.IOException;
import java.net.URISyntaxException;

public class OCRDemo {
	public static void main(String[] args) throws URISyntaxException, UnsupportedOperationException, IOException{
//		TokenDemo();
		AKSKDemo();
		
	}
	public static void TokenDemo() throws URISyntaxException, UnsupportedOperationException, IOException{
		/*
		 * Token demo code
		 * */
		String username="hw_008618752017255_01";
		String password="xxx";
		String domainName="xxx"; //If the current user is not an IAM user, the domainName is the same as the userName.
		String regionName="cn-north-4";
		String httpUri = "/v1.0/ocr/id-card";
		String imgPath = "./data/id-card-demo.jpg"; //File path or URL of the image to be recognized.
		
        // Set params except image
		String sideKey = "side";
		String sideValue = "front";
		JSONObject params = new JSONObject();
		//params.put(sideKey, sideValue);
		try {
			HWOcrClientToken ocrClient= new HWOcrClientToken(domainName, username, password, regionName);
			HttpResponse response=ocrClient.RequestOcrServiceBase64(httpUri, imgPath, params);
			System.out.println(response);
			String content = IOUtils.toString(response.getEntity().getContent(), "utf-8");
			System.out.println(content); 
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void AKSKDemo() throws URISyntaxException, UnsupportedOperationException, IOException{
		/*
		 * AK/SK demo code
		 * */
		String AK="NHZQMMF8WJW3XOH4IUUS"; //AK from authentication
		String SK="Ar5qhzwqMeHUpPiavD2nhXI4MMeLOXfwae8wYQml"; //SK from authentication
		String regionName="cn-north-4"; //region name of the service
//		String httpUri = "/v1.0/ocr/id-card";
		String httpUri = "/v2/cn-north-4/ocr/general-table";
		String imgPath = "./data/id-card-demo.jpg"; //File path or URL of the image to be recognized.
		
        // Set params except image
		String sideKey = "side";
		String sideValue = "front";
		JSONObject params = new JSONObject();
		//params.put(sideKey, sideValue);

		try {
			HWOcrClientAKSK ocrClient=new HWOcrClientAKSK(regionName, AK, SK);
			HttpResponse response=ocrClient.RequestOcrServiceBase64(httpUri, imgPath, params);
			System.out.println(response);
			String content = IOUtils.toString(response.getEntity().getContent(), "utf-8");
			System.out.println(content);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
